/**   
 * @Title: PasswordModifyAct.java 
 * @Package com.esun.cheetah.view.personal
 * @Description: 修改密码
 * @author 程相亭
 * @date 2012-03-23 下午05:07:33  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.view.personal;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.PasswordModifyCommunicater;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.util.EditTextInputUitl;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;
import com.esun.util.md5.MD5;

/**
 * @ClassName: PasswordModifyAct
 * @Description:修改密码
 * @author 程相亭
 * @date 2012-03-23 下午05:07:33
 */
public class PasswordModifyAct extends Activity implements OnClickListener {

	private EditText etOld; // 旧密码
	private EditText etNew; // 新密码
	private EditText etVerify; // 新密码确认

	private Button btnSubmit; // 提交按钮
	private Button btnGoBack; // 返回按钮

	private Button btnClearOld; // 旧密码清除按钮
	private Button btnClearNew; // 新密码清除按钮
	private Button btnClearVerify; // 新密码清除按钮

	private Handler handler;

	// 通讯类
	private PasswordModifyCommunicater passwordModifyCommunicater;

	private ProgressDialog progress;

	private boolean isChangePasswordSuccess = false;
	private String strOld;
	private String strNew;
	private String strVerify;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.password_modify);

		// 初始化
		initHandler();
		initUi();
	}

	/**
	 * @Title: initUi
	 * @Description: UI初始化
	 * @param
	 * @return
	 * @throws
	 */
	public void initUi() {

		btnSubmit = (Button) findViewById(R.id.btn_title_password_modify);
		btnSubmit.setOnClickListener(this);

		btnGoBack = (Button) this.findViewById(R.id.btn_password_modify_goback);
		btnGoBack.setOnClickListener(this);

		btnClearOld = (Button) this
				.findViewById(R.id.btn_change_password_cleanOld);
		btnClearNew = (Button) this
				.findViewById(R.id.btn_change_password_cleanNew);
		btnClearVerify = (Button) this
				.findViewById(R.id.btn_change_password_cleanVerify);

		etOld = (EditText) this.findViewById(R.id.et_change_password_oldPwd);
		etOld = new EditTextInputUitl(etOld, btnClearOld).getEditText();

		etNew = (EditText) this.findViewById(R.id.et_change_password_newPwd);
		etNew = new EditTextInputUitl(etNew, btnClearNew).getEditText();

		etVerify = (EditText) this
				.findViewById(R.id.et_change_password_verifyPwd);
		etVerify = new EditTextInputUitl(etVerify, btnClearVerify)
				.getEditText();

		progress = ViewerUtil.showProgressDialog(this,
				Language.MSG_WAIT_A_MINUTE);
	}

	/**
	 * @Title: initHandler
	 * @Description: Handler初始化
	 * @param
	 * @return
	 * @throws
	 */
	public void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.dismiss();

				switch (msg.what) {

				// 修改密码成功
				case Config.COMMUNICATER_SUCCESS:
					isChangePasswordSuccess = true;
					saveUserinfo();
					ViewerUtil.showTipDialog(PasswordModifyAct.this,
							Language.MSG_PASSWORD_MODIFIED_SUCCESS, true);
					break;

				// 修改密码失败
				case Config.COMMUNICATER_FAIL:
					isChangePasswordSuccess = false;
					ViewerUtil.showTipDialog(PasswordModifyAct.this,
							passwordModifyCommunicater.getServerMsg(), false);
					break;

				// 发生异常
				case Config.COMMUNICATER_EXCEPTION:
					ViewerUtil.showTipDialog(PasswordModifyAct.this,
							Language.MSG_CONNECTIONFAIL, false);
					break;
				}
			}
		};
	}

	/**
	 * @Title: saveUserinfo
	 * @Description: 保存密码
	 * @param
	 * @return
	 * @throws
	 */
	private void saveUserinfo() {

		SharedPreferences sharedPreferences = getSharedPreferences(
				Key.SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putString(Key.SAVE_PASSWORD, MD5.md5(strNew));
		editor.commit();
	}

	/**
	 * @Title: passwordModify
	 * @Description: 修改密码
	 * @param
	 * @return
	 * @throws
	 */
	public void passwordModify() {

		passwordModifyCommunicater = new PasswordModifyCommunicater(handler,
				strOld, strNew);
		passwordModifyCommunicater.startCommunicate();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		// 返回键
		case R.id.btn_password_modify_goback:

			this.finish();

			break;

		// 确认修改
		case R.id.btn_title_password_modify:

			if (checkInput()) {
				progress.show();
				passwordModify();
			}

			break;

		// 密码清空
		case R.id.btn_change_password_cleanOld:

			etOld.setText("");
			v.setVisibility(View.GONE);

			break;

		default:
			break;
		}
	}

	/**
	 * @Title: checkInput
	 * @Description: 检查密码输入
	 * @param
	 * @return
	 * @throws
	 */
	private boolean checkInput() {

		strOld = etOld.getText().toString();
		strNew = etNew.getText().toString();
		strVerify = etVerify.getText().toString();

		if (strOld.length() < 6 || strOld.length() > 15) {
			ViewerUtil.showTipDialog(this, Language.MSG_PASSWORD_LENGTH_ERR,
					false);
			return false;
		} else if (strNew.length() < 6 || strNew.length() > 15) {
			ViewerUtil.showTipDialog(this, Language.MSG_PASSWORD_LENGTH_ERR,
					false);
			return false;
		} else if (strVerify.length() < 6 || strVerify.length() > 15) {
			ViewerUtil.showTipDialog(this, Language.MSG_PASSWORD_LENGTH_ERR,
					false);
			return false;
		} else if (!strNew.equals(strVerify)) {
			ViewerUtil.showTipDialog(this, Language.MSG_PASSWORD_DIFFERENT,
					false);
			return false;
		} else {
			return true;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		EDebug.o(this, "【修改密码页面被关闭】");

		if (isChangePasswordSuccess) {
			// startActivity(new Intent().setClass(ChangePasswordAct.this,
			// LoginAct.class));
		}
	}

}
